*******************************************************************************
* Oklahoma City
*******************************************************************************

use "dataset_other_oklahoma_city", clear

gen B = .
gen SE = .
gen N = .
gen preyear = .

gen jurisdiction = "oklahoma city, oklahoma"
gen design = "Panel"

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust

replace B = _b[_IyeaXpct_2017]
replace SE = _se[_IyeaXpct_2017]
replace N = e(N)
replace preyear = 2017

collapse B SE N year, by(juris design)

save tmp, replace

*******************************************************************************
* Baton Rouge
*******************************************************************************

use "dataset_other_baton_rouge", clear

gen B = .
gen SE = .
gen N = .
gen preyear = 2018

gen jurisdiction = "baton rouge, louisiana"
gen design = "Panel"

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust

replace B = _b[_IyeaXpct_2018]
replace SE = _se[_IyeaXpct_2018]
replace N = e(N)

collapse B SE N preyear, by(juris design)

append using tmp
save tmp, replace

*******************************************************************************
* Other Louisiana parishes
*******************************************************************************

use "dataset_other_louisiana_parishes", clear

gen B = .
gen SE = .
gen N = .
gen preyear = .

gen design = ""
gen jurisdiction = parish

levelsof parish, local(parishes)
foreach parish of local parishes {

	preserve
	
		keep if parish == "`parish'"
		
		su pctblack
		replace pctblack = (pctblack - r(min)) / (r(max) - r(min))
			
		* get number of pre years
		egen npreyearsm = nvals(year) if year < 2020
		egen npreyears = mean(npreyearsm)
		
		* get the first year before 2020
		su year if year < 2020
		local firstpre = r(max)
		* get the second year before 2020
		su year if year < `firstpre'
		local secondpre = r(max)
		
		if "`parish'" != "evangeline parish, louisiana" ///
			xi: reg pctyes i.year*pctblack [aw=totalvotes], robust
		//if "`parish'" == "calcasieu parish, louisiana" x
		
		* parishes with no pre years
		local B = .
		local SE = .
		local N = .
		local design = ""
		
		* parishes with >= 2 pre years
		if npreyears >= 2 & !mi(npreyears) {
				local B = _b[_IyeaXpct_`firstpre']
				local SE = _se[_IyeaXpct_`firstpre']
				local N = e(N)
				local design = "Panel"
		}
			
	restore

	replace B = `B' if parish == "`parish'"
	replace SE = `SE' if parish == "`parish'"	
	replace N = `N' if parish == "`parish'"
	replace preyear = `firstpre' if parish == "`parish'"
	replace design = "`design'" if parish == "`parish'"
		
}

collapse B SE N preyear, by(juris design)

append using tmp
save tmp, replace

*******************************************************************************
* Other Texas cities
*******************************************************************************

use "dataset_other_texas_cities", clear

gen B = .
gen SE = .
gen N = .
gen preyear = .

gen design = "Panel"
gen jurisdiction = city

levelsof city, local(cities)
foreach city of local cities {

	preserve
	
		keep if city == "`city'"
		
		su pctnhblack
		replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))
					
		* get number of pre years
		egen npreyearsm = nvals(year) if year < 2020
		egen npreyears = mean(npreyearsm)
		
		* get the first year before 2020
		su year if year < 2020
		local firstpre = r(max)
		* get the second year before 2020
		su year if year < `firstpre'
		local secondpre = r(max)
	
		if "`city'" != "euless, texas" ///
			xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust
		//if "`city'" == "lewisville, texas" x
		
		* cities with 0 pre years
		local B = .
		local SE = .
		local N = .
		
		* cities with 1 pre year
		su year if year < 2020
		if r(min) == r(max) & r(min) !=. {
			local B = _b[pctnhblack]
			local SE = _se[pctnhblack]
			local N = e(N)
		}
				
		* cities with >= 2 pre years
		su year if year < 2020
		if r(min) != r(max) & r(min) !=. {
			capture local B = _b[_IyeaXpctnh_`firstpre']
			capture local SE = _se[_IyeaXpctnh_`firstpre']
			capture local B = _b[_IyeaXpctn_`firstpre']
			capture local SE = _se[_IyeaXpctn_`firstpre']
			capture local B = _b[_IyeaXpct_`firstpre']
			capture local SE = _se[_IyeaXpct_`firstpre']
			local N = e(N)
		}
					
	restore
	
	if r(min) == r(max) & r(min) !=. replace design = ///
		"Cross-sectional" if city == "`city'"
	replace B = `B' if city == "`city'"
	replace SE = `SE' if city == "`city'"	
	replace N = `N' if city == "`city'"	
	replace preyear = `firstpre' if city == "`city'"
	
}

collapse B SE N preyear, by(juris design)

append using tmp
save tmp, replace

*******************************************************************************
* Main text results
*******************************************************************************

* EBR

use "dataset_east_baton_rouge_cross", replace

gen B = .
gen SE = .
gen N = .

gen jurisdiction = "EBR"
gen design = "Panel"

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust

replace B = _b[_IyeaXpct_2020]
replace SE = _se[_IyeaXpct_2020]
replace N = e(N)

collapse B SE N, by(juris design)

append using tmp
save tmp, replace

* FW

use "dataset_fort_worth_cross", replace

gen B = .
gen SE = .
gen N = .

gen jurisdiction = "FW"
gen design = "Panel"

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust

replace B = _b[_IyeaXpct_2020]
replace SE = _se[_IyeaXpct_2020]
replace N = e(N)

collapse B SE N, by(juris design)

*******************************************************************************
* Combine and graph
*******************************************************************************

append using tmp
erase tmp.dta

gen T = B/SE
gen signif = abs(T) >= 1.96 & !mi(T)

gen UB = B + 1.96*SE
gen LB = B - 1.96*SE

gen lN = log(N)

foreach i in 10 100 1000 {
	local lni = log(`i')
	local myxlab `myxlab' `lni' "`i'"
}	

#delimit;

gr tw
	(sc B lN if signif == 1 & !inlist(juris, "FW", "EBR"), 
		col(gray) msym(O))
	(sc B lN if signif == 0 & !inlist(juris, "EBR", "FW"), 
		col(gray) msym(Oh))
	(rspike UB LB lN if !inlist(juris, "EBR", "FW"), col(gray))
	(sc B lN if inlist(juris, "FW", "EBR"), msym(none) mlab(juris)
		mlabpos(0) mlabcol(black))
	(lfit B lN if !inlist(juris, "EBR", "FW"), 
		col(black) lwid(medthick))
	,
		yline(0, lpat(dash))
		by(design, legend(off) note(""))
		subtitle(, fcol(none) lwid(none))
		ytitle("Point estimate {&plusminus} 2 std. errors")
		xtitle("Number of observations")
		ylab(, angle(horiz))
		plotregion(style(none))
		xlab(`myxlab')
		xscale(range(2 8))
		;

#delimit cr

gr export "_output/figure A23 a.pdf", replace

#delimit;

gr tw
	(sc B preyear if signif == 1, col(gray) msym(O))
	(sc B preyear if signif == 0, col(gray) msym(Oh))
	(rspike UB LB preyear, col(gray))
	(lfit B preyear, col(black) lpat(solid) lwid(medthick))
	,
		yline(0, lpat(dash))
		by(design, legend(off) note(""))
		subtitle(, fcol(none) lwid(none))
		ytitle("Point estimate {&plusminus} 2 std. errors")
		xtitle("Latest Pre-Floyd Year")
		ylab(, angle(horiz))
		plotregion(style(none))
		xlab(2010(1)2019, labsize(vsmall))
		;

#delimit cr

gr export "_output/figure A23 b.pdf", replace

*******************************************************************************
* End
*******************************************************************************
